﻿using System;
using System.Collections.Generic;
using System.Linq;
using CommerceOne.Models.Entities;

namespace CommerceOne.Common
{
    public class Cart
    {
        public Cart()
        {
            ProductLine = new List<CartProduct>();
        }

        /// <summary>
        /// Adds the specified product.
        /// </summary>
        /// <param name="product">The product.</param>
        /// <param name="quantity">The quantity.</param>
        public void Add(Product product, int quantity)
        {
            if (product == null) return;

            var cartProduct = this.ProductLine.Where(p => p.Id == product.ID).FirstOrDefault();
            if (cartProduct == null)
            {
                cartProduct = new CartProduct(product, quantity);
                cartProduct.SubTotal = cartProduct.Price*quantity;
                cartProduct.Quantity = quantity;
                this.ProductLine.Add(cartProduct);
            }
            else
            {
                cartProduct.Quantity += quantity;
                if (cartProduct.Quantity <= 0) this.Remove(product);
                //cartProduct.Price = product.EnablePrice2 ? product.Price2.Value : product.Price1.Value;
                cartProduct.SubTotal = cartProduct.Price*cartProduct.Quantity;
            }
        }

        /// <summary>
        /// Removes the specified product.
        /// </summary>
        /// <param name="product">The product.</param>
        public void Remove(Product product)
        {
            this.ProductLine.RemoveAll(p => p.Id == product.ID);
        }

        /// <summary>
        /// Computes the total.
        /// </summary>
        /// <returns></returns>
        public decimal ComputeTotal()
        {
            return this.ProductLine.Sum(p => p.SubTotal);
        }

        /// <summary>
        /// Computes the total item.
        /// </summary>
        /// <returns></returns>
        public int ComputeTotalItem()
        {
            return this.ProductLine.Sum(p => p.Quantity);
        }

        /// <summary>
        /// Gets the product line.
        /// </summary>
        public List<CartProduct> ProductLine { get; private set; }

        /// <summary>
        /// Clears the cart.
        /// </summary>
        public void ClearCart()
        {
            this.ProductLine = new List<CartProduct>();
        }

        /// <summary>
        /// Gets the last product.
        /// </summary>
        /// <returns></returns>
        public CartProduct GetLastProduct()
        {
            return this.ProductLine.LastOrDefault();
        }

        public decimal ShippingCost { get; set; }
        public bool IsExcludeTax { get; set; }

        private float _taxRate;
        public float TaxRate
        {
            get { return _taxRate; }
            set { _taxRate = value;
                this.Tax = Math.Ceiling((this.ComputeTotal()*this.Tax)/100);
            }
        }

        public decimal Tax { get; set; } 

        public decimal SubTotal
        {
            get { return this.ComputeTotal(); }
        }

        public decimal  Total
        {
            get { return this.SubTotal + this.Tax; }
        }
    }
}
